Logical Operators

Logical Operators will allow us to combine multiple comparison operators. The logical operators we will learn about are:

  • AND - &
  • OR - |
  • NOT - !

The best way to explain these is to see some examples.

In [1]:
# Imagine the variable x
x <- 10

Now we want to know if 10 is less than 20 AND greater than 5:

In [2]:
x < 20
Out[2]:
TRUE
In [3]:
x > 5
Out[3]:
TRUE
In [4]:
x < 20 & x > 5
Out[4]:
TRUE

We can also add parenthesis for readability and to make sure the order of comparisons is what we expect:

In [5]:
(x < 20) & (x>5)
Out[5]:
TRUE
In [6]:
(x < 20) & (x>5) & (x == 10)
Out[6]:
TRUE

We can basically think of this as a series of Logical Boolean values, TRUE & TRUE & TRUE. We return a single TRUE if they are all TRUE. Let's see an example of this not being the case:

In [7]:
x==2 & x > 1
Out[7]:
FALSE

Returned FALSE because while x > 1 is TRUE, we need BOTH to be TRUE, thus the AND statement &. What if we only want one of them to be true? That's when we use OR |. For example:

In [8]:
x==2 | x > 1
Out[8]:
TRUE

Here we only need one or the other to be true!

In [10]:
x==1 | x==12
Out[10]:
FALSE

NOT !

You can think about NOT as reversing any logical value in front of it, basically asking, "Is this NOT true?" For example:

In [13]:
(10==1)
Out[13]:
FALSE
In [14]:
!(10==1)
Out[14]:
TRUE
In [16]:
# We can stack them (pretty uncommon, but possible)
!!(10==1)
Out[16]:
FALSE

Use Case Example

Here's a quick example of a real use case for these operators. Imagine the following data frame:

In [32]:
df <- mtcars
In [33]:
df
Out[33]:
mpgcyldisphpdratwtqsecvsamgearcarb
Mazda RX42161601103.92.6216.460144
Mazda RX4 Wag2161601103.92.87517.020144
Datsun 71022.84108933.852.3218.611141
Hornet 4 Drive21.462581103.083.21519.441031
Hornet Sportabout18.783601753.153.4417.020032
Valiant18.162251052.763.4620.221031
Duster 36014.383602453.213.5715.840034
Merc 240D24.44146.7623.693.19201042
Merc 23022.84140.8953.923.1522.91042
Merc 28019.26167.61233.923.4418.31044
Merc 280C17.86167.61233.923.4418.91044
Merc 450SE16.48275.81803.074.0717.40033
Merc 450SL17.38275.81803.073.7317.60033
Merc 450SLC15.28275.81803.073.78180033
Cadillac Fleetwood10.484722052.935.2517.980034
Lincoln Continental10.4846021535.42417.820034
Chrysler Imperial14.784402303.235.34517.420034
Fiat 12832.4478.7664.082.219.471141
Honda Civic30.4475.7524.931.61518.521142
Toyota Corolla33.9471.1654.221.83519.91141
Toyota Corona21.54120.1973.72.46520.011031
Dodge Challenger15.583181502.763.5216.870032
AMC Javelin15.283041503.153.43517.30032
Camaro Z2813.383502453.733.8415.410034
Pontiac Firebird19.284001753.083.84517.050032
Fiat X1-927.3479664.081.93518.91141
Porsche 914-2264120.3914.432.1416.70152
Lotus Europa30.4495.11133.771.51316.91152
Ford Pantera L15.883512644.223.1714.50154
Ferrari Dino19.761451753.622.7715.50156
Maserati Bora1583013353.543.5714.60158
Volvo 142E21.441211094.112.7818.61142

This shows some data for various car models (its built in to R). Let's grab models with at least 20 mpg:

In [39]:
df[df['mpg'] >= 20,] # Notice the use of indexing with the comma
# subset(df,mpg>=20) # Could also use subset
Out[39]:
mpgcyldisphpdratwtqsecvsamgearcarb
Mazda RX42161601103.92.6216.460144
Mazda RX4 Wag2161601103.92.87517.020144
Datsun 71022.84108933.852.3218.611141
Hornet 4 Drive21.462581103.083.21519.441031
Merc 240D24.44146.7623.693.19201042
Merc 23022.84140.8953.923.1522.91042
Fiat 12832.4478.7664.082.219.471141
Honda Civic30.4475.7524.931.61518.521142
Toyota Corolla33.9471.1654.221.83519.91141
Toyota Corona21.54120.1973.72.46520.011031
Fiat X1-927.3479664.081.93518.91141
Porsche 914-2264120.3914.432.1416.70152
Lotus Europa30.4495.11133.771.51316.91152
Volvo 142E21.441211094.112.7818.61142

Great! Now let's combine filters with logical operators! Let's grab rows with cars of at least 20mpg and over 100 hp.

In [41]:
df[(df['mpg'] >= 20) & (df['hp'] > 100),]
Out[41]:
mpgcyldisphpdratwtqsecvsamgearcarb
Mazda RX42161601103.92.6216.460144
Mazda RX4 Wag2161601103.92.87517.020144
Hornet 4 Drive21.462581103.083.21519.441031
Lotus Europa30.4495.11133.771.51316.91152
Volvo 142E21.441211094.112.7818.61142

Hopefully you know see how useful these type of logical operators can be!

Logical Operators with Vectors

We have two options when use logical operators, a comparison of the entire vectors element by element, or just a comparison of the first elements in the vectors, to make sure the output is a single Logical, don't worry too much about this right now, we will cover it in more depth later on.

In [50]:
tf <- c(TRUE,FALSE)
tt <- c(TRUE,TRUE)
ft <- c(FALSE, TRUE)
In [51]:
tt & tf
Out[51]:
  1. TRUE
  2. FALSE
In [55]:
tt | tf
Out[55]:
  1. TRUE
  2. TRUE

To compare first elements use && or ||

In [56]:
ft && tt
Out[56]:
FALSE
In [57]:
tt && tf
Out[57]:
TRUE
In [58]:
tt || tf
Out[58]:
TRUE
In [59]:
tt || ft
Out[59]:
TRUE

Ok that's it for logical operators! This knowledge will help us when we use it for basic if and else statements!